#include "ControleurEcriture.h"
#include "SDL.h"
#include <iostream>

using namespace std;

ControleurEcriture::ControleurEcriture(Ecran * e)
{
    ecran = e;
    origine = e->getOrigine();
    degre = 0;
    boucle();
}

void ControleurEcriture::cleanEcran()
{
    ecran->setCouleurFond(ecran->getCouleur(255,255,255));
    ecran->drawRepere();
    /*
    ecran->drawLine(Point(0,ecran->getHauteur()/2),Point(ecran->getLargeur(),ecran->getHauteur()/2));
    ecran->drawLine(Point(ecran->getLargeur()/2,0),Point(Point(ecran->getLargeur()/2,ecran->getHauteur())));
    */
}

void ControleurEcriture::boucle()
{
    cout << "Gestion de l'ecriture" << endl;

    SDL_Event event;
    while (true)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {

            case SDL_QUIT:
                exit(0);
            break;

            /*case SDL_MOUSEBUTTONDOWN:
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                {
                    Point p(event.button.x - origine.x  , origine.y - event.button.y);
                    ecran->drawBiggerPixel(p);
                    tabP.push_back(p);
                    ptAnt = p;
                }
            }
            break;*/

            case SDL_MOUSEMOTION:
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                {
                    Point p(event.button.x - origine.x  , origine.y - event.button.y);
                    ecran->drawBiggerPixel(p);
                    ecran->drawLine(p,ptAnt);
                    ptAnt = p;
                    tabP.push_back(p);
                }
            }
            break;

            case SDL_MOUSEBUTTONUP:
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                {
                    Point p(event.button.x - origine.x  , origine.y - event.button.y);
                    ecran->drawBiggerPixel(p);
                    tabP.push_back(p);
                    ecran->drawLine(p,ptAnt);
                    ptAnt = p;

                    cout << "Nb de point: " << tabP.size() << endl;
                }
            }
            break;

            case SDL_KEYDOWN:
            {
                switch (event.key.keysym.sym)
                {
                    case SDLK_c: /* Appui sur la touche ECHAP */
                    {
                        cleanEcran();

                        Vecteur * tVect = new Vecteur[tabP.size()];

                        for(unsigned int i = 0 ; i < tabP.size() ; i ++)
                        {
                            tVect[i] = Vecteur(tabP[i].x , tabP[i].y);
                            ecran->drawBiggerPixel(tabP[i]);
                        }

                        degre = tabP.size() - 1;

                        courbe = Courbe(degre,tVect);
                        cinit = courbe;

                        ecran->drawCourbe(courbe);

                        cout << "Courbe genere de degre: " << degre << endl;
                    }
                    break;

                    case SDLK_s:
                    {
                        sauvegarde();
                    }
                    break;

                    case SDLK_l:
                    {
                        string lettre;
                        cout << "Lettre a charger ?" << endl;
                        cin >> lettre;
                        courbe = Courbe( "Alphabet/" + lettre + "/" + lettre + ".txt");


                        Uint32 c = ecran->getCouleur(255,0,0);


                        courbe.normaliser();
                        Point * tab = courbe.getPtEnglobant();

                        ecran->setCouleurFond(ecran->getCouleur(255,255,255));
                        ecran->drawRepere();
                        ecran->drawCourbe(courbe);

                        ecran->drawLine(Point(0,250),Point(250,250),ecran->getCouleur(0,255,0));
                        ecran->drawLine(Point(250,250),Point(250,0),ecran->getCouleur(0,255,0));

                        ecran->drawLine(tab[0],tab[1],c);
                        ecran->drawLine(tab[2],tab[3],c);
                        ecran->drawLine(tab[0],tab[3],c);
                        ecran->drawLine(tab[1],tab[2],c);

                    }
                    break;

                    case SDLK_DOWN: /* Appui sur la touche FLECHE_BAS */
                    {
                        cout << "Diminution du degre" << endl;
                        cleanEcran();
                        ecran->drawCourbe(cinit);
                        degre--;
                        courbe = courbe--;
                        ecran->drawCourbe(courbe,100,ecran->getCouleur(255,0,0));
                        cout << "Courbe genere de degre: " << degre << endl;


                        /*tabP.clear();

                        Vecteur * v = courbe.getVecteur();

                        for(int i = 0 ; i < degre + 1  ; i++)
                            tabP.push_back(Point(v[i].x,v[i].y));

                        ecran->drawCourbe(courbe);

                        for(unsigned int i = 0 ; i < tabP.size() ; i ++)
                            ecran->drawBiggerPixel(tabP[i]);

                        cout << "Nouveau degre de la courbe: " << degre << endl;*/
                    }
                    break;

                    case SDLK_UP: /* Appui sur la touche FLECHE_HAUT */
                    {
                        cout << "Augmentation du degre" << endl ;
                        degre++;
                        courbe = courbe++;
                        cleanEcran();
                        tabP.clear();

                        Vecteur * v = courbe.getVecteur();

                        for(int i = 0 ; i < degre + 1  ; i++)
                            tabP.push_back(Point(v[i].x,v[i].y));

                        ecran->drawCourbe(courbe);

                        for(unsigned int i = 0 ; i < tabP.size() ; i ++)
                            ecran->drawBiggerPixel(tabP[i]);

                        cout << "Nouveau degre de la courbe: " << degre << endl;
                    }
                    break;

                    case SDLK_ESCAPE: /* Appui sur la touche ECHAP */
                    {
                        exit(1);
                    }
                    break;

                    default:
                        cout << "Contenu du tableau" << endl;
                        for(unsigned int i = 0 ; i < tabP.size() ; i ++)
                            cout << "Point #" << i << " " << (string)tabP[i] << endl;
                    break;
                }
            }
            break;
        }
    }
}

void ControleurEcriture::sauvegarde()
{
    string lettre = "";
    cout << "Lettre ?" << endl;
    cin >> lettre;

    courbe.sauvegarde(lettre);
    ecran->saveBMP("Alphabet/" + lettre + "/" + lettre + ".bmp");
}


